﻿using BBScraper.Model;
using Microsoft.Extensions.Configuration;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks.Dataflow;

namespace BBScraper
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var config = new ConfigurationManager();
            config.AddUserSecrets(typeof(Program).Assembly);
            var url = config.GetSection("MeilisearchUrl").Value;
            var secret = config.GetSection("MasterKey").Value;
            _ = Task.Run(async () =>
            {
                using (var strem = new StreamReader($"{AppContext.BaseDirectory}\\routes.txt", Encoding.UTF8))
                {
                    var i = 1;
                    var option = new ChromeOptions();
                    option.AddArgument("--lang=zh-CN");
                    option.BinaryLocation = $"{AppContext.BaseDirectory}\\chromedriver.exe";
                    option.PageLoadStrategy = PageLoadStrategy.Normal;
                    var driver = new ChromeDriver(option);
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
                    Meilisearch.MeilisearchClient meilisearchClient = new Meilisearch.MeilisearchClient(url, secret);
                    var searchmodels = new List<SearchModel>();
                    var index = meilisearchClient.Index("bbsearch");
                    while (strem.Peek() != -1)
                    {
                        var str = strem.ReadLine();
                        driver.Navigate().GoToUrl(str);
                        try
                        {
                            var title = driver.FindElement(By.TagName("h3"));
                            var subtitle = driver.FindElement(By.TagName("h4"));

                            var searchmodel = new SearchModel
                            {
                                ID = i++,
                                Url = str,
                                Title = title.Text,
                                SubTitle = subtitle.Text
                            };
                            searchmodels.Add(searchmodel);

                            var demoBlock = driver.FindElements(By.CssSelector(".demo-block"));
                            foreach (var item in demoBlock)
                            {
                                var demo = new DemoBlock();
                                var anchor = item.FindElement(By.CssSelector(".anchor-text"));
                                var p = item.FindElement(By.TagName("p"));
                                var id = item.FindElement(By.CssSelector(".anchor-link")).GetAttribute("id");
                                demo.Url = str + "#" + id;
                                demo.AnchorText = anchor.Text;
                                demo.PText = p.Text;
                                searchmodel.DemoBlocks!.Add(demo);
                            }

                           
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    await index.AddDocumentsAsync(searchmodels);
                    driver.Quit();
                    Environment.Exit(0);
                }
            });
            Console.ReadLine();
        }
    }
}
